#include "account.h"
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <display.h>
#include <iostream>
#include <order.h>
#include <string>
#include <cstdlib>
#include <ctime>
#include <account.h>
using namespace std;

OrderManager orderManager("../file/orders.json");
AccountManager accountManager("../file/account.json");

static void title(void)
{   
    time_t now = time(0);  // 获取当前时间戳
    struct tm* localTime = localtime(&now);  // 将时间戳转换为本地时间结构
    int year = localTime->tm_year + 1900;  // 年份（需要加上 1900）
    int month = localTime->tm_mon + 1;  // 月份（范围为 0-11，需要加上 1）
    int day = localTime->tm_mday;  // 当月的第几天
    
    cout<<"============================================"<<endl;
    cout<<"电商订单管理系统"<<endl;
    cout<<"Version: 0.0.1"<<endl;
    cout<<"Author: 奶茶不加冰"<<endl;
    cout<<"最后更新时间: "<<year<<"-"<<month<<"-"<<day<<endl;
    cout<<"============================================"<<endl;
}

/**
 * @brief 新增订单任务函数
 * 
 */
void addOrderMenu(void)
{
    string buyer;
    int status;
    string product;

    system("clear");//清屏
    title();
    
    cout<<"请输入买家信息:"<<endl;
    cout<<">>:";
    cin.ignore();
    getline(cin,buyer);

    cout<<"请选择订单状态:"<<endl;
    cout<<"[1] 准备中"<<endl;
    cout<<"[2] 运输中"<<endl;
    cout<<"[3] 订单完成"<<endl;
    cout<<">>:";
    cin>>status;

    cout<<"请输入商品信息:"<<endl;
    cout<<">>:";
    cin.ignore();
    getline(cin,product);

    switch(status)
    {
        case 1:
            orderManager.addOrder(Order(buyer, product, "准备中"));
            break;
        case 2:
            orderManager.addOrder(Order(buyer, product, "运输中"));
            break;
        case 3:
            orderManager.addOrder(Order(buyer, product, "订单完成"));
            break;
        default:
            break;
    }
    cout<<"订单新建完成"<<endl;
    cout<<"============================================"<<endl;
    cout<<"[0] 退出程序"<<endl;
    cout<<"[9] 返回上一级菜单"<<endl;
    cout<<">>:";
    int option;
    cin>>option;
    cin.ignore();
    switch (option) {
        case 9:
            orderManager.saveOrders();
            MainMenu();
            break;
        case 0:
            orderManager.saveOrders();
            exit(0);
            return;
            break;
    }
}

/**
 * @brief 订单信息修改
 * 
 */
void ModifyOrderMenu(void)
{
    int option;

    system("clear");
    string num;
    title();

    cout<<"请输入待修改订单的订单号:"<<endl;
    cout<<">>:";
    cin.ignore();
    getline(cin,num);

    cout<<"原订单信息:"<<endl;
    cout<<"———————————————————————————————"<<endl;
    if(!orderManager.findOrdersByNum(num))//没有订单
    {
        cout<<"无此订单信息,请输入正确订单编号"<<endl;
        //TODO:需要添加后续处理
    }

    cout<<"请选择要修改的信息:"<<endl;
    cout<<"[1] 买家信息"<<endl;
    cout<<"[2] 订单状态"<<endl;
    cout<<"[3] 商品信息"<<endl;
input1:
    cout<<">>:";
    cin>>option;
    cout<<"———————————————————————————————"<<endl;
    switch (option) 
    {
        case 1:
        {
            string buyer;
            cout<<"请输入修改信息:"<<endl;
            cout<<">>:";
            cin.ignore();
            getline(cin,buyer);
            cout<<"———————————————————————————————"<<endl;
            orderManager.modifyOrderBuyer(num, buyer);
            cout<<"订单信息如下:"<<endl;
            orderManager.findOrdersByNum(num);
            orderManager.saveOrders();
            break;
        }   
        case 2:
        {
            string status;
            cout<<"请输入修改信息:"<<endl;
            cout<<">>:";
            cin.ignore();
            getline(cin,status);
            cout<<"———————————————————————————————"<<endl;
            orderManager.modifyOrderStatus(num, status);
            cout<<"修改后信息:"<<endl;
            orderManager.findOrdersByNum(num);
            orderManager.saveOrders();
            break;
        }
        case 3:
        {
            string product;
            cout<<"请输入修改信息:"<<endl;
            cout<<">>:";
            cin.ignore();
            getline(cin,product);
            cout<<"———————————————————————————————"<<endl;
            orderManager.modifyOrderProduct(num, product);
            cout<<"修改后信息:"<<endl;
            orderManager.findOrdersByNum(num);
            orderManager.saveOrders();
            break;
        }    
        default:
        {
            cout<<"非有效选项，请重新选择!"<<endl;
            goto input1;
            break;
        }   
    }
    cout<<"修改完成！"<<endl;
end:
    cout<<"============================================"<<endl;
    cout<<"[0] 退出程序"<<endl;
    cout<<"[9] 返回上一级菜单"<<endl;
    
input:
    cout<<">>:";
    cin>>option;
    switch(option)
    {
        case 9:
        {
            MainMenu();
            break;
        }
        case 0:
        {
            orderManager.saveOrders();
            exit(0);
            return;
            break;
        }
        default:
        {
            cout<<"非有效选项，请重新选择！"<<endl;
            goto input;
            break;
        }
            
    }
    return;  
}

/**
 * @brief 列举出数据库中的全部订单  
 * 
 */
void ListOrderMenu(void)
{
    system("clear");
    title();
    orderManager.displayOrders();
    cout<<"============================================"<<endl;
    cout<<"[0] 退出程序"<<endl;
    cout<<"[9] 返回上一级菜单"<<endl;
    
input:
    int option;
    cout<<">>:";
    cin>>option;
    switch(option)
    {
        case 9:
        {
            MainMenu();
            break;
        }
        case 0:
        {
            orderManager.saveOrders();
            return;
            break;
        }
        default:
        {
            cout<<"非有效选项，请重新选择！"<<endl;
            goto input;
            break;
        }
            
    }
    return; 
}

void FindOrderMenu(void)
{
    system("clear");
    title();

    cout<<"请选择查找方式:"<<endl;
    input_1:
    cout<<"[1] 按买家信息查找"<<endl;
    cout<<"[2] 按订单编号查找"<<endl;
    cout<<"[3] 返回上一级菜单"<<endl;
    cout<<">>:";
    int option;
    cin>>option;
    switch (option) 
    {
        case 1:
        {   
            string buyer;
            cout<<"请输入买家信息:"<<endl;
            cout<<">>:";
            cin.ignore();
            getline(cin,buyer);
            cout<<"订单信息如下:"<<endl;
            if(orderManager.findOrdersByBuyer(buyer))
            {
                break;
            }
            else//没查询到
            {
                cout<<"查询失败，请重新选择查询类型!"<<endl;
                goto input_1;
            }
        }
        case 2:
        {
            string num;
            cout<<"请输入订单编号信息:"<<endl;
            cout<<">>:";
            cin.ignore();
            getline(cin,num);
            cout<<"订单信息如下:"<<endl;
            if(orderManager.findOrdersByNum(num))
            {
                break;
            }
            else//没查询到
            {
                cout<<"查询失败，请重新选择查询类型!"<<endl;
                goto input_1;
            }
            
            break;
        }
        case 3:
        {
            MainMenu();
            break;
        }
        default:
        {
            cout<<"非有效选项，请重新选择！"<<endl;
            goto input_1;
            break;
        }
    }
    cout<<"查询完毕，请选择下一步操作！"<<endl;
    cout<<"============================================"<<endl;
    cout<<"[0] 退出程序"<<endl;
    cout<<"[9] 返回上一级菜单"<<endl;
    
input:
    cout<<">>:";
    cin>>option;
    switch(option)
    {
        case 9:
        {
            MainMenu();
            break;
        }
        case 0:
        {
            orderManager.saveOrders();
            exit(0);
            return;
            break;
        }
        default:
        {
            cout<<"非有效选项，请重新选择！"<<endl;
            goto input;
            break;
        }
            
    }
    return; 

}

void MainMenu(void)
{
    int option=0;

    system("clear");
    title();

    cout<<"[1] 新增订单"<<endl;
    cout<<"[2] 查看订单列表"<<endl;
    cout<<"[3] 修改订单信息"<<endl;
    cout<<"[4] 查找订单"<<endl;
    cout<<"[0] 退出程序"<<endl;
    cout<<"============================================"<<endl;

input:
    cout<<">>:";
    cin>>option;
    switch(option)
    {
        case 0:
        {
            orderManager.saveOrders();
            exit(0);
            return;
            break;
        }   
        case 1:
        {
            addOrderMenu();
            break;
        }
        case 2:
        {
            ListOrderMenu();//列出全部订单信息
            break;
        }

        case 3:
        {
            ModifyOrderMenu();//修改订单信息
            break;
        }

        case 4:
        {
            FindOrderMenu();//查找订单
        }
        default:
        {
            cout<<"非有效选项，请重新选择！"<<endl;
            goto input;
            break;
        }      
    }

    return;
}


void StartMenu(void)
{
    int option=0;

    system("clear");
    title();

input:
    cout<<"[1] 登录"<<endl;
    cout<<"[2] 注册"<<endl;
    cout<<"[0] 退出程序"<<endl;
    cout<<"============================================"<<endl;
    cout<<">>:";
    cin>>option;
    switch(option)
    {
        case 0:
        {
            accountManager.Save();
            exit(0);
            return;
            break;
        }

        case 1:
        {
            LoginMenu();
            break;
        }

        case 2:
        {
            RegsiterMenu();
            break;
        }

        default:
        {
            cout<<"非有效选项，请重新选择！"<<endl;
            goto input;
            break;
        }
    }


    return;
}

#include <unistd.h>
void LoginMenu(void)
{   
    int option=0;

    system("clear");
    title();

    string id;
    string pwd;
    cout<<"请输入账户ID:"<<endl;
    cout<<">>:";
    cin.ignore();
    getline(cin,id);
    cout<<"请输入账户密码:"<<endl;
    cout<<">>:";
    getline(cin,pwd);

    if(accountManager.Verify(id, pwd))
    {
        cout<<"登录成功,稍后进入系统"<<endl;
        sleep(2);
        MainMenu();
    }
    else 
    {
        cout<<"账号或密码输入错误,自动返回上一级菜单尝试"<<endl;
        cout<<"============================================"<<endl;
        sleep(2);
        StartMenu();
    }

}

void RegsiterMenu(void)
{
    int option=0;
    system("clear");
    title();

    string id;
    string pwd;

    cout<<"请输入账户ID:"<<endl;
    cout<<">>:";
    cin.ignore();
    getline(cin,id);
    cout<<"请输入账户密码:"<<endl;
    cout<<">>:";
    getline(cin,pwd);

    if(accountManager.Add_account(account(id, pwd)))
    {
        cout<<"注册成功,请返回页面登录"<<endl;
        sleep(2);
        StartMenu();
    }
    else 
    {
        cout<<"账户已存在,请返回登录"<<endl;
        cout<<"============================================"<<endl;
        StartMenu();
    }
    
}

